﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.MetadirectoryServices;
using Microsoft.MetadirectoryServices.DetachedObjectModel;
using Lithnet.GoogleApps.ManagedObjects;
using Google.Apis.Admin.Directory.directory_v1.Data;
using Lithnet.MetadirectoryServices;

namespace Lithnet.GoogleApps.MA.UnitTests
{
    [TestClass]
    public class UnitTestGroup
    {
        public UnitTestGroup()
        {
            UnitTestCore.Initialize();
        }

        private CSEntryChange BuildBasicGroup()
        {
            CSEntryChangeDetached csentry = new CSEntryChangeDetached(Guid.NewGuid(), ObjectModificationType.Unconfigured, MAImportError.Success, null);
            csentry.DN = string.Format("{0}@{1}", Guid.NewGuid().ToString(), Properties.Settings.Default.TestDomainSuffix);
            csentry.ObjectModificationType = ObjectModificationType.Add;
            csentry.ObjectType = "group";

            IList<object> aliases = new List<object>() 
                {
                    string.Format("{0}@{1}", Guid.NewGuid().ToString(), Properties.Settings.Default.TestDomainSuffix),
                    string.Format("{0}@{1}", Guid.NewGuid().ToString(), Properties.Settings.Default.TestDomainSuffix)
                };


            csentry.CreateAttributeAdd("primaryEmail", csentry.DN);
            csentry.CreateAttributeAdd("aliases", aliases);
            csentry.CreateAttributeAdd("name", "test1");
            csentry.CreateAttributeAdd("description", "test2");

            csentry.CreateAttributeAdd("maxMessageBytes", 50000);
            csentry.CreateAttributeAdd("includeInGlobalAddressList", true);
            csentry.CreateAttributeAdd("allowExternalMembers", true);
            csentry.CreateAttributeAdd("allowGoogleCommunication", true);
            csentry.CreateAttributeAdd("allowWebPosting", true);
            csentry.CreateAttributeAdd("archiveOnly", false);
            csentry.CreateAttributeAdd("isArchived", true);
            csentry.CreateAttributeAdd("membersCanPostAsTheGroup", true);
            csentry.CreateAttributeAdd("sendMessageDenyNotification", true);
            csentry.CreateAttributeAdd("showInGroupDirectory", true);
            csentry.CreateAttributeAdd("defaultMessageDenyNotificationText", "nope");
            csentry.CreateAttributeAdd("messageDisplayFont", "FIXED_WIDTH_FONT");
            csentry.CreateAttributeAdd("messageModerationLevel", "MODERATE_NON_MEMBERS");
            csentry.CreateAttributeAdd("primaryLanguage", "en-GB");
            csentry.CreateAttributeAdd("replyTo", "REPLY_TO_CUSTOM");
            csentry.CreateAttributeAdd("customReplyTo", "test@test.com");
            csentry.CreateAttributeAdd("spamModerationLevel", "ALLOW");
            csentry.CreateAttributeAdd("whoCanContactOwner", "ALL_MANAGERS_CAN_CONTACT");
            csentry.CreateAttributeAdd("whoCanInvite", "ALL_MANAGERS_CAN_INVITE");
            csentry.CreateAttributeAdd("whoCanJoin", "CAN_REQUEST_TO_JOIN");
            csentry.CreateAttributeAdd("whoCanLeaveGroup", "ALL_MANAGERS_CAN_LEAVE");
            csentry.CreateAttributeAdd("whoCanPostMessage", "ALL_MEMBERS_CAN_POST");
            csentry.CreateAttributeAdd("whoCanViewGroup", "ANYONE_CAN_VIEW");
            csentry.CreateAttributeAdd("whoCanViewMembership", "ALL_MANAGERS_CAN_VIEW");


            return csentry;
        }

        private CSEntryChange UpdateBasicGroup(string dn)
        {
            CSEntryChangeDetached csentry = new CSEntryChangeDetached(Guid.NewGuid(), ObjectModificationType.Unconfigured, MAImportError.Success, null);
            csentry.DN = dn;
            csentry.ObjectModificationType = ObjectModificationType.Update;
            csentry.ObjectType = "group";

            IList<object> aliases = new List<object>() 
                {
                    string.Format("{0}@{1}", Guid.NewGuid().ToString(), Properties.Settings.Default.TestDomainSuffix),
                    string.Format("{0}@{1}", Guid.NewGuid().ToString(), Properties.Settings.Default.TestDomainSuffix)
                };

            csentry.CreateAttributeReplace("primaryEmail", csentry.DN);
            csentry.CreateAttributeReplace("aliases", aliases);
            csentry.CreateAttributeReplace("name", "test3");
            csentry.CreateAttributeReplace("description", "test4");

            csentry.CreateAttributeReplace("maxMessageBytes", 60000);
            csentry.CreateAttributeReplace("includeInGlobalAddressList", false);
            csentry.CreateAttributeReplace("allowExternalMembers", false);
            csentry.CreateAttributeReplace("allowGoogleCommunication", false);
            csentry.CreateAttributeReplace("allowWebPosting", false);
            csentry.CreateAttributeReplace("archiveOnly", false);
            csentry.CreateAttributeReplace("isArchived", false);
            csentry.CreateAttributeReplace("membersCanPostAsTheGroup", false);
            csentry.CreateAttributeReplace("sendMessageDenyNotification", false);
            csentry.CreateAttributeReplace("showInGroupDirectory", false);
            csentry.CreateAttributeDelete("defaultMessageDenyNotificationText");
            csentry.CreateAttributeReplace("messageDisplayFont", "FIXED_WIDTH_FONT");
            csentry.CreateAttributeReplace("messageModerationLevel", "MODERATE_NON_MEMBERS");
            csentry.CreateAttributeReplace("primaryLanguage", "de");
            csentry.CreateAttributeReplace("replyTo", "REPLY_TO_IGNORE");
            csentry.CreateAttributeDelete("customReplyTo");
            csentry.CreateAttributeReplace("spamModerationLevel", "MODERATE");
            csentry.CreateAttributeReplace("whoCanContactOwner", "ANYONE_CAN_CONTACT");
            csentry.CreateAttributeReplace("whoCanInvite", "ALL_MEMBERS_CAN_INVITE");
            csentry.CreateAttributeReplace("whoCanJoin", "ANYONE_CAN_JOIN");
            csentry.CreateAttributeReplace("whoCanLeaveGroup", "ALL_MEMBERS_CAN_LEAVE");
            csentry.CreateAttributeReplace("whoCanPostMessage", "ALL_IN_DOMAIN_CAN_POST");
            csentry.CreateAttributeReplace("whoCanViewGroup", "ALL_MANAGERS_CAN_VIEW");
            csentry.CreateAttributeReplace("whoCanViewMembership", "ALL_MEMBERS_CAN_VIEW");


            return csentry;
        }

        [TestMethod]
        public void GroupAddBasic()
        {
            CSEntryChange csentry = this.BuildBasicGroup();

            try
            {
                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                EvaluateBasicGroup(csentry.DN, csentry.GetValueAdds<string>("aliases"));
            }
            finally
            {
                UnitTestCore.DeleteGroup(csentry.DN);
            }
        }

        [TestMethod]
        public void GroupUpdateBasic()
        {
            CSEntryChange csentry = this.BuildBasicGroup();
            IList<string> aliases = csentry.GetValueAdds<string>("aliases");
            try
            {
                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                EvaluateBasicGroup(csentry.DN, aliases);

                csentry = this.UpdateBasicGroup(csentry.DN);
                aliases = csentry.GetValueAdds<string>("aliases");
                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                EvaluateBasicGroup2(csentry.DN, aliases);

            }
            finally
            {
                UnitTestCore.DeleteGroup(csentry.DN);
            }
        }

        private static GoogleGroup EvaluateBasicGroup(string dn, IList<string> expectedAliases)
        {
            GoogleGroup g = new GoogleGroup();

            Group group = GroupRequestFactory.Get(dn);
            GroupSettings settings = GroupSettingsRequestFactory.Get(dn);
            GroupMembership membership = GroupMemberRequestFactory.GetMembership(dn);

            g.Group = group;
            g.Settings = settings;
            g.Membership = membership;

            Assert.AreEqual("test1", group.Name);
            Assert.AreEqual("test2", group.Description);
            CollectionAssert.AreEquivalent(expectedAliases.ToArray(), group.Aliases.ToArray());

            Assert.AreEqual(50000, settings.MaxMessageBytes);
            Assert.AreEqual(true, settings.IncludeInGlobalAddressList);
            Assert.AreEqual(true, settings.AllowExternalMembers);
            Assert.AreEqual(true, settings.AllowGoogleCommunication);
            Assert.AreEqual(true, settings.AllowWebPosting);
            Assert.AreEqual(false, settings.ArchiveOnly);
            Assert.AreEqual(true, settings.IsArchived);
            Assert.AreEqual(true, settings.MembersCanPostAsTheGroup);
            Assert.AreEqual(true, settings.SendMessageDenyNotification);
            Assert.AreEqual(true, settings.ShowInGroupDirectory);
            Assert.AreEqual("nope", settings.DefaultMessageDenyNotificationText);
            Assert.AreEqual("FIXED_WIDTH_FONT", settings.MessageDisplayFont);
            Assert.AreEqual("MODERATE_NON_MEMBERS", settings.MessageModerationLevel);
            Assert.AreEqual("en-GB", settings.PrimaryLanguage);
            Assert.AreEqual("REPLY_TO_CUSTOM", settings.ReplyTo);
            Assert.AreEqual("test@test.com", settings.CustomReplyTo);
            Assert.AreEqual("ALLOW", settings.SpamModerationLevel);
            Assert.AreEqual("ALL_MANAGERS_CAN_CONTACT", settings.WhoCanContactOwner);
            Assert.AreEqual("ALL_MANAGERS_CAN_INVITE", settings.WhoCanInvite);
            Assert.AreEqual("CAN_REQUEST_TO_JOIN", settings.WhoCanJoin);
            Assert.AreEqual("ALL_MANAGERS_CAN_LEAVE", settings.WhoCanLeaveGroup);
            Assert.AreEqual("ALL_MEMBERS_CAN_POST", settings.WhoCanPostMessage);
            Assert.AreEqual("ANYONE_CAN_VIEW", settings.WhoCanViewGroup);
            Assert.AreEqual("ALL_MANAGERS_CAN_VIEW", settings.WhoCanViewMembership);

            return g;
        }

        private static void EvaluateBasicGroup2(string dn, IList<string> expectedAliases)
        {

            Group group = GroupRequestFactory.Get(dn);
            GroupSettings settings = GroupSettingsRequestFactory.Get(dn);
            GroupMembership membership = GroupMemberRequestFactory.GetMembership(dn);
            Assert.AreEqual("test3", group.Name);
            Assert.AreEqual("test4", group.Description);
            CollectionAssert.AreEquivalent(expectedAliases.ToArray(), group.Aliases.ToArray());

            Assert.AreEqual(60000, settings.MaxMessageBytes);
            Assert.AreEqual(false, settings.IncludeInGlobalAddressList);
            Assert.AreEqual(false, settings.AllowExternalMembers);
            Assert.AreEqual(false, settings.AllowGoogleCommunication);
            Assert.AreEqual(false, settings.AllowWebPosting);
            Assert.AreEqual(false, settings.ArchiveOnly);
            Assert.AreEqual(false, settings.IsArchived);
            Assert.AreEqual(false, settings.MembersCanPostAsTheGroup);
            Assert.AreEqual(false, settings.SendMessageDenyNotification);
            Assert.AreEqual(false, settings.ShowInGroupDirectory);
            Assert.IsNull(settings.DefaultMessageDenyNotificationText);
            Assert.AreEqual("FIXED_WIDTH_FONT", settings.MessageDisplayFont);
            Assert.AreEqual("MODERATE_NON_MEMBERS", settings.MessageModerationLevel);
            Assert.AreEqual("de", settings.PrimaryLanguage);
            Assert.AreEqual("REPLY_TO_IGNORE", settings.ReplyTo);
            Assert.IsNull(settings.CustomReplyTo);
            Assert.AreEqual("MODERATE", settings.SpamModerationLevel);
            Assert.AreEqual("ANYONE_CAN_CONTACT", settings.WhoCanContactOwner);
            Assert.AreEqual("ALL_MEMBERS_CAN_INVITE", settings.WhoCanInvite);
            Assert.AreEqual("ANYONE_CAN_JOIN", settings.WhoCanJoin);
            Assert.AreEqual("ALL_MEMBERS_CAN_LEAVE", settings.WhoCanLeaveGroup);
            Assert.AreEqual("ALL_IN_DOMAIN_CAN_POST", settings.WhoCanPostMessage);
            Assert.AreEqual("ALL_MANAGERS_CAN_VIEW", settings.WhoCanViewGroup);
            Assert.AreEqual("ALL_MEMBERS_CAN_VIEW", settings.WhoCanViewMembership);
        }

        [TestMethod]
        public void GroupAddBasicMembers()
        {
            CSEntryChange csentry = this.BuildBasicGroup();
            string user1 = UnitTestCore.CreateUser();
            string user2 = UnitTestCore.CreateUser();
            string user3 = UnitTestCore.CreateUser();

            try
            {
                csentry.CreateAttributeAdd("member", user1);
                csentry.CreateAttributeAdd("manager", user2);
                csentry.CreateAttributeAdd("owner", user3);
                csentry.CreateAttributeAdd("externalMember", "user1@lithiumblue.com");
                csentry.CreateAttributeAdd("externalManager", "user2@lithiumblue.com");
                csentry.CreateAttributeAdd("externalOwner", "user3@lithiumblue.com");

                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                GoogleGroup g = EvaluateBasicGroup(csentry.DN, csentry.GetValueAdds<string>("aliases"));

                Assert.AreEqual(1, g.Membership.Members.Count);
                Assert.AreEqual(user1, g.Membership.Members.First());

                Assert.AreEqual(1, g.Membership.Managers.Count);
                Assert.AreEqual(user2, g.Membership.Managers.First());

                Assert.AreEqual(1, g.Membership.Owners.Count);
                Assert.AreEqual(user3, g.Membership.Owners.First());


                Assert.AreEqual(1, g.Membership.ExternalMembers.Count);
                Assert.AreEqual("user1@lithiumblue.com", g.Membership.ExternalMembers.First());

                Assert.AreEqual(1, g.Membership.ExternalManagers.Count);
                Assert.AreEqual("user2@lithiumblue.com", g.Membership.ExternalManagers.First());

                Assert.AreEqual(1, g.Membership.ExternalOwners.Count);
                Assert.AreEqual("user3@lithiumblue.com", g.Membership.ExternalOwners.First());
            }
            finally
            {
                UnitTestCore.DeleteGroup(csentry.DN);
                UnitTestCore.DeleteUser(user1);
                UnitTestCore.DeleteUser(user2);
                UnitTestCore.DeleteUser(user3);
            }
        }

        [TestMethod]
        public void GroupUpdateBasicMembersUpdate()
        {
            CSEntryChange csentry = this.BuildBasicGroup();
            string user1 = UnitTestCore.CreateUser();
            string user2 = UnitTestCore.CreateUser();
            string user3 = UnitTestCore.CreateUser();
            string user4 = UnitTestCore.CreateUser();
            string user5 = UnitTestCore.CreateUser();
            string user6 = UnitTestCore.CreateUser();
            string id = csentry.DN;

            try
            {
                csentry.CreateAttributeAdd("member", user1);
                csentry.CreateAttributeAdd("manager", user2);
                csentry.CreateAttributeAdd("owner", user3);
                csentry.CreateAttributeAdd("externalMember", "user1@lithiumblue.com");
                csentry.CreateAttributeAdd("externalManager", "user2@lithiumblue.com");
                csentry.CreateAttributeAdd("externalOwner", "user3@lithiumblue.com");

                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                IList<string> aliases = csentry.GetValueAdds<string>("aliases");
                GoogleGroup g = EvaluateBasicGroup(csentry.DN, aliases);

                Assert.AreEqual(1, g.Membership.Members.Count);
                Assert.AreEqual(user1, g.Membership.Members.First());

                Assert.AreEqual(1, g.Membership.Managers.Count);
                Assert.AreEqual(user2, g.Membership.Managers.First());

                Assert.AreEqual(1, g.Membership.Owners.Count);
                Assert.AreEqual(user3, g.Membership.Owners.First());


                Assert.AreEqual(1, g.Membership.ExternalMembers.Count);
                Assert.AreEqual("user1@lithiumblue.com", g.Membership.ExternalMembers.First());

                Assert.AreEqual(1, g.Membership.ExternalManagers.Count);
                Assert.AreEqual("user2@lithiumblue.com", g.Membership.ExternalManagers.First());

                Assert.AreEqual(1, g.Membership.ExternalOwners.Count);
                Assert.AreEqual("user3@lithiumblue.com", g.Membership.ExternalOwners.First());


                csentry = CSEntryChange.Create();
                csentry.ObjectModificationType = ObjectModificationType.Update;
                csentry.ObjectType = "group";
                csentry.DN = id;

                List<ValueChange> memberChanges = new List<ValueChange>();
                memberChanges.Add(ValueChange.CreateValueAdd(user4));
                memberChanges.Add(ValueChange.CreateValueAdd(user5));
                memberChanges.Add(ValueChange.CreateValueAdd(user6));
                memberChanges.Add(ValueChange.CreateValueDelete(user1));

                csentry.CreateAttributeUpdate("member", memberChanges);

                csentry.CreateAttributeUpdate("externalMember", new List<ValueChange>() { ValueChange.CreateValueAdd("user4@lithiumblue.com") });

                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                g = EvaluateBasicGroup(csentry.DN, aliases);
                Assert.AreEqual(3, g.Membership.Members.Count);
                CollectionAssert.AreEquivalent(new string[] { user4, user5, user6 }, g.Membership.Members.ToArray());

                Assert.AreEqual(1, g.Membership.Managers.Count);
                Assert.AreEqual(user2, g.Membership.Managers.First());

                Assert.AreEqual(1, g.Membership.Owners.Count);
                Assert.AreEqual(user3, g.Membership.Owners.First());

                Assert.AreEqual(2, g.Membership.ExternalMembers.Count);
                CollectionAssert.AreEquivalent(new string[] { "user1@lithiumblue.com", "user4@lithiumblue.com" }, g.Membership.ExternalMembers.ToArray());

                Assert.AreEqual(1, g.Membership.ExternalManagers.Count);
                Assert.AreEqual("user2@lithiumblue.com", g.Membership.ExternalManagers.First());

                Assert.AreEqual(1, g.Membership.ExternalOwners.Count);
                Assert.AreEqual("user3@lithiumblue.com", g.Membership.ExternalOwners.First());
            }
            finally
            {
                UnitTestCore.DeleteGroup(csentry.DN);
                UnitTestCore.DeleteUser(user1);
                UnitTestCore.DeleteUser(user2);
                UnitTestCore.DeleteUser(user3);
                UnitTestCore.DeleteUser(user4);
                UnitTestCore.DeleteUser(user5);
                UnitTestCore.DeleteUser(user6);
            }
        }

        [TestMethod]
        public void GroupUpdateBasicMembersDelete()
        {
            CSEntryChange csentry = this.BuildBasicGroup();
            string user1 = UnitTestCore.CreateUser();
            string user2 = UnitTestCore.CreateUser();
            string user3 = UnitTestCore.CreateUser();
            string user4 = UnitTestCore.CreateUser();
            string user5 = UnitTestCore.CreateUser();
            string user6 = UnitTestCore.CreateUser();
            string id = csentry.DN;

            try
            {
                List<object> members = new List<object>() { user1, user4, user5, user6 };
                csentry.CreateAttributeAdd("member", members);
                csentry.CreateAttributeAdd("manager", user2);
                csentry.CreateAttributeAdd("owner", user3);
                csentry.CreateAttributeAdd("externalMember", "user1@lithiumblue.com");
                csentry.CreateAttributeAdd("externalManager", "user2@lithiumblue.com");
                csentry.CreateAttributeAdd("externalOwner", "user3@lithiumblue.com");

                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                IList<string> aliases = csentry.GetValueAdds<string>("aliases");
                GoogleGroup g = EvaluateBasicGroup(csentry.DN, aliases);

                Assert.AreEqual(4, g.Membership.Members.Count);
                CollectionAssert.AreEquivalent(members.ToArray(), g.Membership.Members.ToArray());

                Assert.AreEqual(1, g.Membership.Managers.Count);
                Assert.AreEqual(user2, g.Membership.Managers.First());

                Assert.AreEqual(1, g.Membership.Owners.Count);
                Assert.AreEqual(user3, g.Membership.Owners.First());


                Assert.AreEqual(1, g.Membership.ExternalMembers.Count);
                Assert.AreEqual("user1@lithiumblue.com", g.Membership.ExternalMembers.First());

                Assert.AreEqual(1, g.Membership.ExternalManagers.Count);
                Assert.AreEqual("user2@lithiumblue.com", g.Membership.ExternalManagers.First());

                Assert.AreEqual(1, g.Membership.ExternalOwners.Count);
                Assert.AreEqual("user3@lithiumblue.com", g.Membership.ExternalOwners.First());


                csentry = CSEntryChange.Create();
                csentry.ObjectModificationType = ObjectModificationType.Update;
                csentry.ObjectType = "group";
                csentry.DN = id;

                csentry.CreateAttributeDelete("member");

                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                g = EvaluateBasicGroup(csentry.DN, aliases);
                Assert.AreEqual(0, g.Membership.Members.Count);

                Assert.AreEqual(1, g.Membership.Managers.Count);
                Assert.AreEqual(user2, g.Membership.Managers.First());

                Assert.AreEqual(1, g.Membership.Owners.Count);
                Assert.AreEqual(user3, g.Membership.Owners.First());

                Assert.AreEqual(1, g.Membership.ExternalMembers.Count);
                Assert.AreEqual("user1@lithiumblue.com", g.Membership.ExternalMembers.First());

                Assert.AreEqual(1, g.Membership.ExternalManagers.Count);
                Assert.AreEqual("user2@lithiumblue.com", g.Membership.ExternalManagers.First());

                Assert.AreEqual(1, g.Membership.ExternalOwners.Count);
                Assert.AreEqual("user3@lithiumblue.com", g.Membership.ExternalOwners.First());
            }
            finally
            {
                UnitTestCore.DeleteGroup(csentry.DN);
                UnitTestCore.DeleteUser(user1);
                UnitTestCore.DeleteUser(user2);
                UnitTestCore.DeleteUser(user3);
                UnitTestCore.DeleteUser(user4);
                UnitTestCore.DeleteUser(user5);
                UnitTestCore.DeleteUser(user6);
            }
        }

        [TestMethod]
        public void GroupUpdateBasicMembersReplace()
        {
            CSEntryChange csentry = this.BuildBasicGroup();
            string user1 = UnitTestCore.CreateUser();
            string user2 = UnitTestCore.CreateUser();
            string user3 = UnitTestCore.CreateUser();
            string user4 = UnitTestCore.CreateUser();
            string user5 = UnitTestCore.CreateUser();
            string user6 = UnitTestCore.CreateUser();
            string id = csentry.DN;

            try
            {
                List<object> members = new List<object>() { user1, user4, user5 };
                csentry.CreateAttributeAdd("member", members);
                csentry.CreateAttributeAdd("manager", user2);
                csentry.CreateAttributeAdd("owner", user3);
                csentry.CreateAttributeAdd("externalMember", "user1@lithiumblue.com");
                csentry.CreateAttributeAdd("externalManager", "user2@lithiumblue.com");
                csentry.CreateAttributeAdd("externalOwner", "user3@lithiumblue.com");

                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                IList<string> aliases = csentry.GetValueAdds<string>("aliases");
                GoogleGroup g = EvaluateBasicGroup(csentry.DN, aliases);

                Assert.AreEqual(3, g.Membership.Members.Count);
                CollectionAssert.AreEquivalent(members.ToArray(), g.Membership.Members.ToArray());

                Assert.AreEqual(1, g.Membership.Managers.Count);
                Assert.AreEqual(user2, g.Membership.Managers.First());

                Assert.AreEqual(1, g.Membership.Owners.Count);
                Assert.AreEqual(user3, g.Membership.Owners.First());


                Assert.AreEqual(1, g.Membership.ExternalMembers.Count);
                Assert.AreEqual("user1@lithiumblue.com", g.Membership.ExternalMembers.First());

                Assert.AreEqual(1, g.Membership.ExternalManagers.Count);
                Assert.AreEqual("user2@lithiumblue.com", g.Membership.ExternalManagers.First());

                Assert.AreEqual(1, g.Membership.ExternalOwners.Count);
                Assert.AreEqual("user3@lithiumblue.com", g.Membership.ExternalOwners.First());


                csentry = CSEntryChange.Create();
                csentry.ObjectModificationType = ObjectModificationType.Update;
                csentry.ObjectType = "group";
                csentry.DN = id;

                csentry.CreateAttributeReplace("member", user6);

                UnitTestCore.PutCSEntryAndThrowOnError(csentry);

                g = EvaluateBasicGroup(csentry.DN, aliases);
                Assert.AreEqual(1, g.Membership.Members.Count);
                Assert.AreEqual(user6, g.Membership.Members.First());

                Assert.AreEqual(1, g.Membership.Managers.Count);
                Assert.AreEqual(user2, g.Membership.Managers.First());

                Assert.AreEqual(1, g.Membership.Owners.Count);
                Assert.AreEqual(user3, g.Membership.Owners.First());

                Assert.AreEqual(1, g.Membership.ExternalMembers.Count);
                Assert.AreEqual("user1@lithiumblue.com", g.Membership.ExternalMembers.First());

                Assert.AreEqual(1, g.Membership.ExternalManagers.Count);
                Assert.AreEqual("user2@lithiumblue.com", g.Membership.ExternalManagers.First());

                Assert.AreEqual(1, g.Membership.ExternalOwners.Count);
                Assert.AreEqual("user3@lithiumblue.com", g.Membership.ExternalOwners.First());
            }
            finally
            {
                UnitTestCore.DeleteGroup(csentry.DN);
                UnitTestCore.DeleteUser(user1);
                UnitTestCore.DeleteUser(user2);
                UnitTestCore.DeleteUser(user3);
                UnitTestCore.DeleteUser(user4);
                UnitTestCore.DeleteUser(user5);
                UnitTestCore.DeleteUser(user6);
            }
        }
    }
}